package l1j.server.server.model.damage.magic;

import static l1j.server.server.model.skill.L1SkillId.*;
import l1j.server.server.ActionCodes;
import l1j.server.server.model.L1Character;
import l1j.server.server.model.Instance.L1ItemInstance;
import l1j.server.server.model.Instance.L1PcInstance;

import l1j.server.server.model.damage.L1Dmg;
import l1j.server.server.model.damage.L1MagicOnPc;
import l1j.server.server.model.damage.L1PcMagic;
import l1j.server.server.serverpackets.S_DoActionGFX;
import l1j.server.server.serverpackets.S_SkillSound;
import l1j.server.server.templates.L1Skills;
import l1j.server.server.utils.Random;

public class L1MagicDamage_Pc_Pc extends L1Dmg {

	private L1PcInstance _attacker;

	private L1PcInstance _target;

	private int _attr;

	public L1MagicDamage_Pc_Pc(L1PcInstance attacker, L1PcInstance target,
			int attr) {
		_attr = attr;
		_attacker = attacker;
		_target = target;
	}

	public void commitMagic(L1Character target) {
		if (Random.chance100(mr_reduction_chance[target.getMr() + 100])) {
			mul((100 - mr_reduction_percent[target.getMr() + 100]) / 100.0);
		}
		if (_attr == 1) {// 地屬性
			mul((100 - attr_reduction_percent[target.getEarth() + 300]) / 100.0);
		} else if (_attr == 2) {// 火屬性
			mul((100 - attr_reduction_percent[target.getFire() + 300]) / 100.0);
		} else if (_attr == 4) {// 水屬性
			mul((100 - attr_reduction_percent[target.getWater() + 300]) / 100.0);
		} else if (_attr == 8) {// 風屬性
			mul((100 - attr_reduction_percent[target.getWind() + 300]) / 100.0);
		}
	}
	
	// 武器屬性階位傷害
	public void commitAttrByWp(double dmg) {
		// 傷害
		L1ItemInstance weapon = _attacker.getWeapon();
		int chance = 1 + (weapon.getEnchantLevel() / 2);
		double attrDmg = 0;
		int weaponAttrKind = weapon.getAttrEnchantKind();
		int weaponAttrLevel = weapon.getAttrEnchantLevel();
		if (Random.chance100(chance)) {
			int targetAttrValue = 0;
			switch(weaponAttrKind) {
			case 1:
				targetAttrValue = _target.getEarth();
				break;
			case 2:
				targetAttrValue = _target.getFire();
				break;
			case 4:
				targetAttrValue = _target.getWater();
				break;
			case 8:
				targetAttrValue = _target.getWind();
				break;
			default :
				;
				break;
			}
			if (weaponAttrLevel * 10 > targetAttrValue) {
				attrDmg = dmg * (weaponAttrLevel * 10 - targetAttrValue) / 100;
			}
		}
		add(attrDmg);
	}

	@Override
	public void hurtTarget(boolean actionHurt) {
		commitAttacker(_attacker);
		commitTarget(_target);
		commitMagic(_target);
		_target.receiveDamage(_attacker, getValue());
		if (getValue() > 0 && actionHurt) {
			_target.sendPackets(new S_DoActionGFX(_target.getId(),
					ActionCodes.ACTION_Damage));
			_target.broadcastPacket(new S_DoActionGFX(_target.getId(),
					ActionCodes.ACTION_Damage));
		}
	}

	@Override
	public void calcDmg() {
		if (_target.isInvincible()) {
			return;
		}
		L1Skills skill = _attacker.getCurrentSkill();
		if (skill == null) {
			zero();
			return;
		}
		_attr = skill.getAttr();
		L1PcMagic pm = new L1PcMagic(_attacker, this);
		pm.calcPcDmg();

		int skillId = _attacker.getCurrentSkill().getId();
		// 心靈破壞
		if (skillId == MIND_BREAK && _target.getCurrentMp() >= 5) {
			_target.setCurrentMp(_target.getCurrentMp() - 5);
			add(_attacker.getWis() * 5);
		}

		L1MagicOnPc mop = new L1MagicOnPc(_target, this);
		mop.calcOnPcDmg();

		if (_target.hasSkillEffect(COUNTER_MIRROR)) {
			if (_target.getWis() >= Random.nextInt(100)) {
				L1Dmg.get(true, _attr, _target, _attacker).add(getValue())
						.hurtTarget(true);
				_target.sendPackets(new S_SkillSound(_target.getId(), 4395));
				_target.broadcastPacket(new S_SkillSound(_target.getId(), 4395));
				zero();
				_target.killSkillEffectTimer(COUNTER_MIRROR);
			}
		}
	}
}
